package com.midea.ai.air.view;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Canvas;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PathEffect;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;

import com.midea.ai.air.R;

/**
 * Created by hww on 2016/4/26.
 * 电量报告显示View
 */
public class ElectricReportView extends View {

    private int mWidth, mHeight; // View的宽高
    private Paint mPaintOrdText, mPaintBlue, mPaintDash; // 画笔
    private Paint mPaint_bg;

    private int mValueMax = 10, mValueMin = 0; // 电量的最大值和最小值

    private List<Float> mCenterX = new ArrayList<>(); // 圆的X坐标
    private int mRadius; // 圆的半径
    private int mSpace;// 圆柱间距离

    private float mOrTextMaxWidth; // 纵坐标文本的最大宽度

    private List<Float> mElecList = new ArrayList<>(); // 用电量

    private float mPixPerDegree; // 1度占多少像素

    public ElectricReportView(Context context) {
        this(context, null, 0);
    }

    public ElectricReportView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ElectricReportView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        mRadius = 12 / 2;
        mSpace = 8;
        mPixPerDegree = (float) (244) / (float) (mValueMax - mValueMin);

        initPaint();
        initData();

    }

    /**
     * 初始化X坐标和Y坐标
     */
    private void initData() {
        mElecList.add(7.53f);
        mElecList.add(8.5f);
        mElecList.add(7.73f);
        mElecList.add(3.8f);
        mElecList.add(5.2f);
        mElecList.add(5.7f);
        mElecList.add(6.1f);
        mElecList.add(6.7f);
        mElecList.add(7.1f);
        mElecList.add(6.5f);
        mElecList.add(2f);
        mElecList.add(1.5f);
        mElecList.add(1f);
        mElecList.add(0.85f);
        mElecList.add(1.89f);
        mElecList.add(2.3f);
        mElecList.add(2.5f);
        mElecList.add(5f);
        mElecList.add(6.6f);
        mElecList.add(5.3f);
        mElecList.add(4.8f);
        mElecList.add(4f);
        mElecList.add(3.3f);
        mElecList.add(4f);
        mElecList.add(5f);
        mElecList.add(4.6f);
        mElecList.add(2.5f);
        mElecList.add(2f);
        mElecList.add(1.89f);
        mElecList.add(1.7f);

        // 求取纵坐标文本的最大宽度
        float one = mPaintOrdText.measureText(mValueMax + "");
        float two = mPaintOrdText.measureText(mValueMin + "");
        if (one > two) {
            mOrTextMaxWidth = one;
        } else {
            mOrTextMaxWidth = two;
        }
    }

    /**
     * 初始化画笔
     */
    public void initPaint() {
        Resources resources = getResources();

        // 纵坐标文本画笔
        mPaintOrdText = new Paint();
        mPaintOrdText.setColor(resources.getColor(R.color.electric_show_text));
        mPaintOrdText.setAntiAlias(true);
        mPaintOrdText
                .setTextSize(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PX, 20, resources.getDisplayMetrics()));
        mPaintOrdText.setTextAlign(Paint.Align.RIGHT);

        // 柱状画笔
        mPaintBlue = new Paint();
        // 颜色待定
        mPaintBlue.setColor(resources.getColor(R.color.electric_show_context));
        mPaintBlue.setAntiAlias(true);
        mPaintBlue.setStyle(Paint.Style.FILL);

        // 柱状背景
        mPaint_bg = new Paint();
        mPaint_bg.setColor(resources.getColor(R.color.electric_show_bg));
        mPaint_bg.setAntiAlias(true);
        mPaint_bg.setStyle(Paint.Style.FILL);

        // 虚线画笔
        mPaintDash = new Paint();
        mPaintDash.setColor(resources.getColor(R.color.electric_show_text));
        mPaintDash.setAntiAlias(true);
        mPaintDash.setStyle(Paint.Style.STROKE);
        PathEffect effects = new DashPathEffect(new float[]{4, 8}, 1); // 画出类似虚线的线
        mPaintDash.setPathEffect(effects);
        mPaintDash.setStrokeWidth(2);

    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mWidth = w;
        mHeight = h;
        // 初始化横坐标列表的值

        float space = mRadius * 2 + mSpace;
        for (int i = 0; i < 30; i++) {
            mCenterX.add(45f + mRadius + space * i);
        }
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        // 绘制虚线
        Path path = new Path();
        path.moveTo(0, mPixPerDegree * 7);
        path.lineTo(mWidth, 7 * mPixPerDegree);
        canvas.drawPath(path, mPaintDash);

        // drawCircle(canvas);
        // drawAbscissaText(canvas);
        drawOrdinateText(canvas);
        drawHistogram_me(canvas);
        drawHistogram(canvas);
    }

    /**
     * 绘制纵坐标文本
     */
    private void drawOrdinateText(Canvas canvas) {

        float top = Math.abs(mPaintOrdText.getFontMetrics().top);
        float bottom = Math.abs(mPaintOrdText.getFontMetrics().bottom);
        float space = top - (top + bottom) / 2f;
        // canvas.drawText(mValueMax + "", mWidth - (mOrTextMaxWidth / 2), 45f +
        // space, mPaintOrdText);
        // canvas.drawText(mValueMin + "", mWidth - (mOrTextMaxWidth / 2), 45f *
        // 4 + space, mPaintOrdText);
        canvas.drawText(10 + "", mWidth + (mOrTextMaxWidth / 2) - 45, space * 3, mPaintOrdText);
        canvas.drawText(0 + "", mWidth + (mOrTextMaxWidth / 2) - 45, mHeight - space, mPaintOrdText);
    }

    /**
     * 绘制柱状图
     */
    private void drawHistogram(Canvas canvas) {
        RectF rect = new RectF();
        float axisX;

        for (int i = 0; i < mElecList.size(); i++) {
            axisX = mCenterX.get(i);

            rect.left = axisX - mRadius;
            rect.right = axisX + mRadius;
            rect.top = (mValueMax - mElecList.get(i) - mValueMin) * mPixPerDegree;
            rect.bottom = mHeight;

            canvas.drawRoundRect(rect, mRadius, mRadius, mPaintBlue);
        }
    }

    /**
     * 绘制柱状图背景
     *
     * @param canvas
     */
    private void drawHistogram_me(Canvas canvas) {
        RectF rect = new RectF();
        float axisX;

        for (int i = 0; i < mElecList.size(); i++) {
            axisX = mCenterX.get(i);

            rect.left = axisX - mRadius;
            rect.right = axisX + mRadius;
            rect.top = 0;
            rect.bottom = mHeight;

            canvas.drawRoundRect(rect, mRadius, mRadius, mPaint_bg);
        }
    }
}